package com.ruoyi.web.controller.bus.controller;

import com.ruoyi.bus.domain.BusData;
import com.ruoyi.bus.dto.BusDataAuditResult;
import com.ruoyi.bus.dto.BusDataResult;
import com.ruoyi.bus.service.IBusDataAuditService;
import com.ruoyi.bus.service.IBusDataService;
import com.ruoyi.bus.service.IBusEnterpriseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-09-01
 */
@Controller
@RequestMapping("/bus/data")
public class BusDataController extends BaseController
{
    private String prefix = "bus/data";
    @Autowired
    private IBusDataService busDataService;
    @Autowired
    private IBusEnterpriseService busEnterpriseService;
   @Autowired
   private IBusDataAuditService busDataAuditService;

    @RequiresPermissions("bus:data:view")
    @GetMapping()
    public String data()
    {
        return prefix + "/data";
    }
    @RequiresPermissions("bus:data:minute")
    @GetMapping("/minute")
    public String minute()
    {
        return prefix + "/minute";
    }

    /**
     * 查询【请填写功能名称】列表
     */


@RequestMapping("selectAlert")
    public int selectAlert(Long monitorId){
        Integer num=0;
        List<BusDataResult> busDataResults = busDataService.selectAlert(monitorId);
        for(BusDataResult bdr:busDataResults){
            if( bdr.getAdFlag().equals(1)||bdr.getCodFlag().equals(1)||bdr.getPhFlag().equals(1)||bdr.getCondFlag().equals(1)||bdr.getCrFlag().equals(1)||bdr.getRyFlag().equals(1)||bdr.getTnFlag().equals(1)||bdr.getWdFlag().equals(1)||bdr.getZlFlag().equals(1)
                    ||bdr.getYlsFlag().equals(1)||bdr.getZdFlag().equals(1)){
                num++;
            }
        }
        return num;
    }
    @RequiresPermissions("bus:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusData busData)
    {
        startPage();
        List<BusDataResult> result = new ArrayList<>();
        List<BusDataResult> busDataResults = busDataService.selectBusDataList(busData);
        List<BusDataAuditResult> busDataAuditResults = busDataAuditService.selectFactorAndAudit();
        Map<String, List<BusDataAuditResult>> mapByTime = busDataAuditResults.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(BusDataAuditResult::getMoniterId));
        for (BusDataResult bd : busDataResults) {
            BusDataResult bdr=new BusDataResult();
            if (mapByTime.containsKey(String.valueOf(bd.getMonitorId()))) {
                List<BusDataAuditResult> list = mapByTime.get(String.valueOf(bd.getMonitorId()));
                if (!CollectionUtils.isEmpty(list)) {
                    for(BusDataAuditResult bda:list){
                        switch (bda.getFactorCode()) {
                            case "NH3-N-S":
                                if(bd.getAd()!=null){
                                    if( new BigDecimal(bd.getAd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getAd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setAdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "TP":
                                if(bd.getZl()!=null){
                                    if( new BigDecimal(bd.getZl()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getZl()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setZlFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "COD":
                                if(bd.getCod()!=null){
                                    if( new BigDecimal(bd.getCod()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCod()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCodFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "PH":
                                if(bd.getPh()!=null){
                                    if( new BigDecimal(bd.getPh()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getPh()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setPhFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "CODMn":
                                if(bd.getCond()!=null){
                                    if( new BigDecimal(bd.getCond()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCond()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCondFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "DO":
                                if(bd.getRy()!=null){
                                    if( new BigDecimal(bd.getRy()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getRy()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setRyFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "FTU":
                                if(bd.getZd()!=null){
                                    if( new BigDecimal(bd.getZd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getZd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setZdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "TEMP":
                                if(bd.getWd()!=null){
                                    if( new BigDecimal(bd.getWd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getWd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setWdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "CR":
                                if(bd.getCr()!=null){
                                    if( new BigDecimal(bd.getCr()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCr()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCrFlag("1");

                                    };
                                }else{
                                    break;
                                }
                            default:;
                        }
                    }
                }
            }
            BeanUtils.copyProperties(bd,bdr);
            result.add(bdr);
        }
        return getDataTable(busDataResults);
    }
    @PostMapping("/monitorName")
    @ResponseBody
    public List<BusData> selectMonitorName(BusData busData)
    {
        List<BusData> monitorName = busDataService.selectMonitorName(busData);
        System.out.println(monitorName);
        return monitorName;
    }

    @PostMapping("/busDataMonth")
    @ResponseBody
    public TableDataInfo selectBusDataMonth(BusData busData)
    {
        startPage();
        List<BusDataResult> result = new ArrayList<>();
        List<BusDataResult> busData1 = busDataService.selectBusDataListMn(busData);
        List<BusDataAuditResult> busDataAudits = busDataAuditService.selectFactorAndAudit();
        Map<String, List<BusDataAuditResult>> mapByTime = busDataAudits.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(BusDataAuditResult::getMoniterId));
        for (BusDataResult bd : busData1) {
            BusDataResult bdr=new BusDataResult();
            if (mapByTime.containsKey(String.valueOf(bd.getMonitorId()))) {
                List<BusDataAuditResult> list = mapByTime.get(String.valueOf(bd.getMonitorId()));
                if (!CollectionUtils.isEmpty(list)) {
                    for(BusDataAuditResult bda:list){
                        switch (bda.getFactorCode()) {
                            case "NH3-N-S":
                                if(bd.getAd()!=null){
                                    if( new BigDecimal(bd.getAd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getAd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setAdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "TP":
                                if(bd.getZl()!=null){
                                    if( new BigDecimal(bd.getZl()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getZl()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setZlFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "COD":
                                if(bd.getCod()!=null){
                                    if( new BigDecimal(bd.getCod()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCod()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCodFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "PH":
                                if(bd.getPh()!=null){
                                    if( new BigDecimal(bd.getPh()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getPh()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setPhFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "CODMn":
                                if(bd.getCond()!=null){
                                    if( new BigDecimal(bd.getCond()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCond()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCondFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "DO":
                                if(bd.getRy()!=null){
                                    if( new BigDecimal(bd.getRy()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getRy()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setRyFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "FTU":
                                if(bd.getZd()!=null){
                                    if( new BigDecimal(bd.getZd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getZd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setZdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "TEMP":
                                if(bd.getWd()!=null){
                                    if( new BigDecimal(bd.getWd()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getWd()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setWdFlag("1");
                                    };
                                }else{
                                    break;
                                }

                            case "CR":
                                if(bd.getCr()!=null){
                                    if( new BigDecimal(bd.getCr()).compareTo(new BigDecimal(bda.getMaxValue())) > 0
                                            || new BigDecimal(bd.getCr()).compareTo(new BigDecimal(bda.getMinValue())) < 0){
                                        bd.setCrFlag("1");

                                    };
                                }else{
                                    break;
                                }
                            default:;
                        }
                    }
                }
            }
            BeanUtils.copyProperties(bd,bdr);
            result.add(bdr);
            System.out.println(result);
        }
        return getDataTable(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/list/mn")
    @ResponseBody
    public TableDataInfo listMn(BusData busData)
    {
        startPage();
        List<BusDataResult> list = busDataService.selectBusDataListMn(busData);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/listDay")
    @ResponseBody
    public TableDataInfo listDay(BusData busData)
    {
        startPage();
        List<BusData> list = busDataService.selectBusDataDay(busData);
        return getDataTable(list);
    }
    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/listMonth")
    @ResponseBody
    public TableDataInfo listMonth(BusData busData)
    {
        startPage();
        List<BusData> list = busDataService.selectBusDataMonth(busData);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequestMapping("/day/{monitorId}/{produceTime}")
    @ResponseBody
    public TableDataInfo listDay(@PathVariable("monitorId") Long monitorId,@PathVariable("produceTime") String produceTime)
    {
        startPage();
        List<BusData> list = busDataService.selectBusDataByDay(monitorId,produceTime);
        return getDataTable(list);
    }

    /**
     * 查询单个【请填写功能名称】列表
     */
    @GetMapping("/show/{monitorId}")
    public String show1(@PathVariable("monitorId") Long monitorId, ModelMap mmap)
    {
        BusData busData = busDataService.selectBusDataByMonitorId(monitorId);
        mmap.put("busData", busData);
        return prefix + "/show";
    }

    /**
     * 查询单个【请填写功能名称】列表
     */
    @GetMapping("/ent/{monitorId}")
    @ResponseBody
    public List select(@PathVariable("monitorId") Long monitorId)
    {
        List<BusData> ListData = busEnterpriseService.selectBusEnterpriseByMonitorId(monitorId);
        return ListData;
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("bus:data:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusData busData)
    {
        List<BusDataResult> list = busDataService.selectBusDataList(busData);
        ExcelUtil<BusDataResult> util = new ExcelUtil<BusDataResult>(BusDataResult.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("bus:data:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusData busData)
    {
        return toAjax(busDataService.insertBusData(busData));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{collectId}")
    public String edit(@PathVariable("collectId") Long collectId, ModelMap mmap)
    {
        BusData busData = busDataService.selectBusDataByCollectId(collectId);
        mmap.put("busData", busData);
        return prefix + "/show";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("bus:data:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusData busData)
    {
        return toAjax(busDataService.updateBusData(busData));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("bus:data:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busDataService.deleteBusDataByCollectIds(ids));
    }
}